<?php
require_once 'init.php';
$phpmongo = new Phpmongo();
?>

<?php
// create db
if ($_POST['act'] == 'createdb'){
	$name = $_POST['db'];
	$phpmongo->createDb($name);
	message("New database created.", "db.php?db=$name");
}
?>

<?php
// drop db
if ($_GET['act'] == 'dropDb'){
	$name = $_GET['db'];
	$phpmongo->dropDb($name);
	message("database $name dropped.", "index.php");
}
?>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>PhpMongo</title>
<link href="css/style.css" rel="stylesheet" type="text/css">
<script type="text/javascript" src="js/common.js"></script>
</head>

<body>
<div class="warp_left">
<?php
require_once 'left.php';
?>	
</div>
<div class="warp_right">
		<div>
			<a href="db.php?act=dropDb&db=<?php echo $_GET['db'];?>"
			onclick="return window.confirm('Caution:are you sure to drop database <?php echo $_GET['db'];?>?All data in the db will be losed!');"
			><b>Drop datebase <?php echo $_GET['db'];?></b></a>
			<br /><br /><br />
			<form action="collection.php" method="POST" onsubmit="return check();">
				<table>
					<tr>
						<td><b>Create Collection  </b></td>
					</tr>
					<tr>
						<td>New collection *:</td>
						<td>
						<input type="text" name="collection" id="collection" />
						</td>
					</tr>
					<tr>
						<td>Is Capped:</td>
						<td>
						<input type="checkbox" name="capped" value="1" />
						</td>
					</tr>
					<tr>
						<td>Size:</td>
						<td>
						<input type="text" name="size"  />
						</td>
					</tr>
					<tr>
						<td>Max:</td>
						<td>
						<input type="text" name="max" />
						</td>
					</tr>
					<tr>
						<td>
						<input type="hidden" name="db" value="<?php echo $_GET['db'];?>" />
						<input type="hidden" name="act" value="createcollection" />
						<input type="submit" value="Create collection"  class="submit" /></td>
					</tr>
				</table>
			</form>
<script type="text/javascript">
function check(){
	var collection = document.getElementById('collection').value;
	collection = trim(collection);
	if(collection == ''){
		alert("please input collection name");
		document.getElementById('collection').value = '';
		document.getElementById('collection').focus();
		return false;
	}
}
</script>			
		</div>
		
	<div>
		<table class="table_info" <?php if ($phpmongo->countCollection($_GET['db']) == 0){echo ' style="display:none;" ';};?>>
			<tr class="mingxi_table_thead">
				<th colspan="12">Database <?php echo $_GET['db'];?> Info</th>
			</tr>
			<tr>
				<th>Collection</th>
				<th>count</th>
				<th>size</th>
				<th>storageSize</th>
				<th>numExtents</th>
				<th>nindexes</th>
				<th>lastExtentSize</th>
				<th>paddingFactor</th>
				<th>flags</th>
				<th>totalIndexSize</th>
				<th>indexSizes</th>
				<th>ok</th>
			</tr>
			<?php
			$collections = $phpmongo->getCollection($_GET['db']);
			foreach ($collections as $collection):
			$collectionInfo = $phpmongo->collectionInfo($_GET['db'], $collection);
			?>
			<tr>
				<td>
				<a href="collection.php?db=<?php echo $_GET['db'];?>&collection=<?php echo $collection;?>"><?php echo $collection;?></a>
				
				<div class="dbaction">
					<a href="collection.php?db=<?php echo $_GET['db'];?>&collection=<?php echo $collection;?>"><img src="images/ico-show.gif" title="View" alt="view" /></a>
					<a onclick="return window.confirm('Are you sure to drop collecion <?php echo $collection;?>?All data in the collection will be losed!');"
			href="collection.php?act=dropCollection&db=<?php echo $_GET['db'];?>&collection=<?php echo $collection;?>">
					<img src="images/ico-delete.gif" title="delete" alt="delete" />
					</a>
					<a onclick="return window.confirm('Are you sure to clear all records in collection <?php echo $collection;?>?');"
			href="collection.php?act=clearCollection&db=<?php echo $_GET['db'];?>&collection=<?php echo $collection;?>">
					<img src="images/ico_clear.gif" title="clear" alt="clear" />
					</a>
				</div>				
				</td>
				<td><?php echo $collectionInfo['count'];?></td>
				<td><?php echo $collectionInfo['size'];?></td>
				<td><?php echo $collectionInfo['storageSize'];?></td>
				<td><?php echo $collectionInfo['numExtents'];?></td>
				<td><?php echo $collectionInfo['nindexes'];?></td>
				<td><?php echo $collectionInfo['lastExtentSize'];?></td>
				<td><?php echo $collectionInfo['paddingFactor'];?></td>
				<td><?php echo $collectionInfo['flags'];?></td>
				<td><?php echo $collectionInfo['totalIndexSize'];?></td>
				<td><?php print_r ($collectionInfo['indexSizes']);?></td>
				<td><?php echo $collectionInfo['ok'];?></td>
			</tr>	
			<?php
			endforeach;
			?>
	</div>
</div>	
</body>
</html>
